package com.person.webdemoproject2.controller;

import com.person.webdemoproject2.pojo.Notification;
import com.person.webdemoproject2.pojo.Result;
import com.person.webdemoproject2.service.NotificationService;
import com.person.webdemoproject2.utils.jwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@CrossOrigin

@Slf4j
@RestController
@RequestMapping("/notice")
public class NoticeController {
    @Autowired
    NotificationService notificationService;

    @GetMapping("/check")
    public Result getUnreadNotifications(@RequestHeader String token){
        log.info("查询用户的未读通知");

        Integer userId = jwtUtils.getUserId(token);

        List<Notification> notifications = notificationService.getUnreadNotifications(userId);

        return Result.success(notifications);
    }
    @PostMapping("/markAsRead")
    public Result markNotificationAsRead(@RequestHeader String token, @RequestParam int notificationId) {
        log.info("标记通知为已读, 通知ID: {}", notificationId);

        // 假设通过 token 获取用户的 ID
        Integer userId = jwtUtils.getUserId(token);

        // 标记通知为已读
        notificationService.markAsRead(notificationId);

        return Result.success("通知已标记为已读");
    }


}

